package com.da.lon.wang.xlg.adapter;

import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;
import android.widget.TextView;

import com.da.lon.wang.xlg.MyApplication;
import com.da.lon.wang.xlg.R;
import com.da.lon.wang.xlg.bean.SingleSaleGoods;
import com.da.lon.wang.xlg.util.ViewHolder;

import java.util.List;

public class SingleSaleAdapter extends BaseAdapter {

    private LayoutInflater inflater;
    private List<SingleSaleGoods> goods;
    private Activity context;

    public SingleSaleAdapter(List<SingleSaleGoods> list, Activity context) {

        inflater = LayoutInflater.from(context);
        this.context = context;
        this.goods = list;
    }

    @Override
    public int getCount() {
        return goods != null ? goods.size() : 0;
    }

    @Override
    public Object getItem(int position) {
        return goods != null && goods.size() > position ? goods.get(position)
                : null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        try {
            if (goods != null && goods.size() > position
                    && goods.get(position) != null) {
                SingleSaleGoods good = goods.get(position);
                if (position % 2 == 0) {
                    convertView = inflater.inflate(
                            R.layout.ouka_buyer_singlesale_listviewitem1, null);

                } else {
                    convertView = inflater.inflate(
                            R.layout.ouka_buyer_singlesale_listviewitem2, null);
                }

                TextView nameTextView = ViewHolder.get(convertView,
                        R.id.singlesale_list_name);
                TextView titleTextView = ViewHolder.get(convertView,
                        R.id.singlesale_list_title);
                TextView priceTextView = ViewHolder.get(convertView,
                        R.id.singlesale_list_price);
                TextView measureTextView = ViewHolder.get(convertView,
                        R.id.singlesale_list_measure);

                nameTextView.setText(good.getName());
                titleTextView.setText(good.getSub_title());
                priceTextView.setText(good.getPricespe() + "");
                measureTextView.setText("/" + good.getMeasure());

                ImageView listImageView = ViewHolder.get(convertView,
                        R.id.singlesale_list_img);
                listImageView.setScaleType(ScaleType.CENTER_INSIDE);
                if (good.getBigimage() != null
                        && !"".equals(good.getBigimage()))
                    MyApplication.loadImageByPicasso(context, good.getBigimage(), null, listImageView, 0, 0, 0);
//					MyApplication.loadImage(context, good.getBigimage(),
//							listImageView);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return convertView;
    }

}
